package ch11;

import java.util.ArrayList;
import java.util.Collections;

class _01_ArrayListEx {	
	public static void main(String[] args){
		ArrayList<Integer> list1 = new ArrayList<Integer>(10);
		list1.add(5);
		list1.add(4);
		list1.add(2);
		list1.add(0);
		list1.add(1);
		list1.add(3);

		ArrayList<Integer> list2 = new ArrayList<Integer>(list1.subList(1, 4));
		print(list1, list2); // list1:[5, 4, 2, 0, 1, 3] ,list2:[4, 2, 0]

		Collections.sort(list1);
		Collections.sort(list2);
		print(list1, list2); // list1:[0, 1, 2, 3, 4, 5] ,list2:[0, 2, 4]
				
		list1.set(3, 7);
		print(list1, list2); // list1:[0, 1, 2, 7, 4, 5] ,list2:[0, 2, 4]
		
		System.out.println("list1.containsAll(list2):" + list1.containsAll(list2)); // list1.containsAll(list2):true
		System.out.println("list1.retainAll(list2):" + list1.retainAll(list2)); // list1.retainAll(list2):true
		print(list1, list2); // list1:[0, 2, 4] ,list2:[0, 2, 4]
		
		for (int i = list2.size()-1; i >= 0; i--) {
			if (list1.contains(list2.get(i)))
				list2.remove(i); 
		}
		print(list1, list2); // list1:[0, 2, 4] ,list2:[]
	}

	static void print(ArrayList<Integer> list1, ArrayList<Integer> list2) {
		System.out.println("list1:" + list1 + " ,list2:" + list2);
		System.out.println();
	}
}